package com.spring.boot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.boot.entity.ChinaCity;
import com.spring.boot.entity.InOutNum;
import com.spring.boot.entity.InOutNumCurve;
import com.spring.boot.mapper.InOutNumMapper;
import com.spring.boot.service.InOutNumService;
import com.spring.boot.utils.CityId;
import com.spring.boot.utils.MyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.spring.boot.utils.MyUtils.*;

@Service
public class InOutNumServiceImpl extends ServiceImpl<InOutNumMapper, InOutNum> implements InOutNumService {
    private final static Logger log = LoggerFactory.getLogger(InOutNumServiceImpl.class);

    @Override
    public boolean mySaveOrUpdateBatch(List<InOutNum> inOutNums) {
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        for (InOutNum inOutNum : inOutNums) {
            inOutNum.setCreateTime(matter.format(new Date()));
        }
        return super.saveOrUpdateBatch(inOutNums);
    }

    @Override
    public Map getDangerLevel(String date) {
        HashMap<String, Object> map = new HashMap<>();//定义结果集
        if (StrUtil.hasEmpty(date) || date.equals("")) {

            //查询福建省每个城市昨天的迁入城市、和每个城市的迁入指数
            for (CityId item : CityId.values()) {
                String cityName = item.getCityName();
                //获取迁入城市
                List<InOutNum> moveInCities = MyUtils.getMoveInCities(cityName,MyUtils.getToday());
                //获取迁入指数
                InOutNumCurve curve = MyUtils.getMoveInNum(cityName,MyUtils.getToday());//福建市级城市迁入指数

                BigDecimal tempResult = new BigDecimal(0);
                //查询这些迁入城市的最新疫情数据
                for (InOutNum moveInCity : moveInCities) {
                    ChinaCity cityInfo = getCityInfo(moveInCity.getCityName(), getToday(),0);
                    tempResult = getDangerLevel(curve, tempResult, moveInCity, cityInfo);
                }
                map.put(cityName + "市", tempResult);
            }
            return map;
        }
        else {
            //查询福建省每个城市昨天的迁入城市、和每个城市的迁入指数
            for (CityId item : CityId.values()) {
                String cityName = item.getCityName();
                //获取迁入城市
                List<InOutNum> moveInCities = MyUtils.getMoveInCities(cityName,date);
                //获取迁入指数
                InOutNumCurve curve = MyUtils.getMoveInNum(cityName,date);//福建市级城市迁入指数

                BigDecimal tempResult = new BigDecimal(0);
                //查询这些迁入城市的最新疫情数据
                for (InOutNum moveInCity : moveInCities) {
                    ChinaCity cityInfo = getCityInfo(moveInCity.getCityName(), date,0);
                    tempResult = getDangerLevel(curve, tempResult, moveInCity, cityInfo);
                }
                map.put(cityName + "市", tempResult);
            }
            return map;
        }

    }

    @Override
    public List<InOutNum> getFlyLineData(String date) {
        QueryWrapper<InOutNum> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("target_city", "city_name", "value");
        HashMap<String, Object> map = new HashMap<>();
        map.put("date", date);
        map.put("province_name", "福建省");
        map.put("type", "in");
        queryWrapper.allEq(map);
        queryWrapper.orderByDesc("value");
        queryWrapper.last("limit 12");//查到太多显示出来会很乱
        List<InOutNum> list = super.list(queryWrapper);
        return list;
    }

    private BigDecimal getDangerLevel(InOutNumCurve curve, BigDecimal tempResult, InOutNum moveInCity, ChinaCity cityInfo) {
        if (!cityInfo.getMark().equals("error")){
            BigDecimal asy = new BigDecimal(cityInfo.getAsymptomatic());
            BigDecimal curConfirm = new BigDecimal(cityInfo.getCurConfirm());
            BigDecimal add = asy.add(curConfirm);
            tempResult = tempResult.add(curve.getValue().multiply(moveInCity.getValue()).multiply(add));
            //curve.getValue() * moveInCity.getValue() * (city.getAsymptomatic() + city.getCurConfirm())
        }
        return tempResult;
    }
}
